package assignment.day3;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class D3A7Validate {
	String name;

	public void setName(String name) {
		this.name = name;
	}

	public boolean display() throws ClassNotFoundException, SQLException,
			NamingException {
		/*
		 * Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con =
		 * DriverManager.getConnection(
		 * "jdbc:oracle:thin:@10.23.0.100:1521:esgdb10", "scott", "tiger");
		 */
		/*
		 * Class.forName("org.postgresql.Driver"); Connection con =
		 * DriverManager.getConnection( "jdbc:postgresql://localhost:5432/jee",
		 * "karthik", "tiger");
		 */
		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup("jdbc/mydata");
		Connection con = ds.getConnection();

		PreparedStatement stmt = con
				.prepareStatement("select * from mywebusers where username=?");
		stmt.setString(1, name);
		ResultSet rs = stmt.executeQuery();
		stmt.close();
		con.close();
		if (rs.next())
			return true;
		else
			return false;

	}
}
